home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / WINSRC20.ZIP / LINE3D.C < prev    next >
Text File  |  1990-09-12  |  57KB  |  1,960 lines

  1. /* This file contains a 3D replacement for the out_line function called
  2.    by the decoder. The purpose is to apply various 3D transformations before
  3.    displaying points. Called once per line of the input file.
  4.  
  5.    This module is linked as an overlay, use ENTER_OVLY and EXIT_OVLY.
  6.  
  7.    Original Author Tim Wegner, with extensive help from Marc Reinig.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <limits.h>
  13. #include <dos.h>
  14. #include "fractint.h"
  15.  
  16. /* routines in this module    */
  17.  
  18. void line3d_overlay(void);
  19. int  line3d();
  20.  
  21. static void vdraw_line(double *,double *,int);
  22. static int  putminmax();
  23. static int  clipcolor(int,int,int);
  24. static int  clipcolor2(int,int,int);
  25. static int  interpcolor(int,int,int);
  26. static int  T_clipcolor(int,int,int);
  27. static int  T_interpcolor(int,int,int);
  28. static void corners(),draw_light_box(),showfpoint();
  29. static int  putatriangle(),offscreen(),H_R(),R_H();
  30. static int  Open_T_24(),startdisk1(),set_pixel_buff();
  31.  
  32. static int TOP = -1;
  33. static int BOT = -1;
  34. static int LEFT = -1;
  35. static int RIGHT = -1;
  36.  
  37. static    int line_length1;
  38. static    int T_header_24 = 18; /* Size of current Targa-24 header */
  39. extern    unsigned char dacbox[256][3];
  40. static    FILE *File_Ptr1 = NULL;
  41. int Ambient;
  42. static unsigned int IAmbient;
  43. int RANDOMIZE;
  44. static int rand_factor;
  45. int haze;
  46. static int HAZE_MULT;
  47. int full_color;
  48. char light_name[80];
  49. extern    int warn;        /* warnings on/off */
  50. unsigned char back_color[3];
  51. static unsigned char Real_Color;
  52. extern int dotmode; /* video access method, 11 if really disk video */
  53. extern int calc_status;
  54. extern long calctime;
  55.  
  56. static int (*fillplot)();
  57. static int (*normalplot)();
  58. int (*standardplot)();
  59.  
  60. char preview = 0;
  61. char showbox = 0;
  62. static int localpreviewfactor;
  63. int previewfactor = 20;
  64. int xadjust = 0;
  65. int yadjust = 0;
  66. int xxadjust;
  67. int yyadjust;
  68. int xshift;
  69. int yshift;
  70. extern int overflow;
  71. extern int filetype;
  72. extern char floatflag;
  73. extern int (*plot)();
  74. extern int xdots, ydots, colors, sxoffs, syoffs;
  75. extern int debugflag;
  76. extern void draw_line (int X1, int Y1, int X2, int Y2, int color);
  77. extern int extraseg;
  78. extern unsigned height;
  79. extern int rowcount;        /* in general.asm */
  80. extern int init3d[];            /* 3D arguments (FRACTINT.C) */
  81. extern long far *lx0;
  82. extern int transparent[2];        /* transparency min/max */
  83. extern int pot16bit;
  84.  
  85. static int zcoord = 256;
  86. static double aspect;           /* aspect ratio */
  87. static int evenoddrow;
  88.  
  89. static float far *sinthetaarray;    /* all sine   thetas go here  */
  90. static float far *costhetaarray;    /* all cosine thetas go here */
  91.  
  92. static double rXrscale;        /* precalculation factor */
  93.  
  94. static int persp;    /* flag for indicating perspective transformations */
  95. int bad_value = -10000; /* set bad values to this */
  96. int bad_check = -1000;    /* check values against this to determine if good */
  97.  
  98. /* this array remembers the previous line */
  99. struct point
  100. {
  101.    int x;
  102.    int y;
  103.    int color;
  104. } far *lastrow;
  105.  
  106. static struct point p1,p2,p3;
  107.  
  108. struct f_point
  109. {
  110.    float x;
  111.    float y;
  112.    float color;
  113. } far *f_lastrow;
  114.  
  115. /* array of min and max x values used in triangle fill */
  116. struct minmax
  117. {
  118.     int minx;
  119.     int maxx;
  120. } far *minmax_x;
  121.  
  122. VECTOR view;    /* position of observer for perspective */
  123. VECTOR cross;
  124. VECTOR tmpcross;
  125.  
  126. void line3d_overlay() { }    /* for restore_active_ovly */
  127.  
  128. int line3d(unsigned char pixels[], unsigned linelen)
  129. {
  130.  
  131. static FILE *out1;
  132. static int numsaves1=0;
  133. char File_Name[12];
  134. int RND;
  135. char openfile[80], lightfiletype[10];
  136.  
  137.    /* these values come from FRACTINT.C */
  138.  
  139.    /* These variables must preserve their values across calls */
  140.    static float   deltaphi;  /* increment of latitude, longitude */
  141.    static float phi;
  142.    static double rscale;          /* surface roughness factor */
  143.    static long xcenter,ycenter;       /* circle center */
  144.    static double sclx,scly,sclz;      /* scale factors */
  145.    static double R;              /* radius values */
  146.    static double Rfactor;          /* for intermediate calculation */
  147.    static MATRIX m;              /* transformation matrix */
  148.    static MATRIX lightm;          /* m w/no trans, keeps obj. on screen */
  149.  
  150.  
  151.    static LMATRIX lm;              /* "" */
  152.    static LVECTOR lview;              /* for perspective views */
  153.    static double zcutoff;          /* perspective backside cutoff value */
  154.    static float twocosdeltaphi;
  155.    static float cosphi,sinphi;            /* precalculated sin/cos of longitude */
  156.    static float oldcosphi1,oldsinphi1;
  157.    static float oldcosphi2,oldsinphi2;
  158.    double r;                  /* sphere radius */
  159.    double xval,yval,zval;          /* rotation values */
  160.    float costheta,sintheta;         /* precalculated sin/cos of latitude */
  161.    float twocosdeltatheta;
  162.  
  163.    int col;                  /* current column (original GIF) */
  164.    struct point cur;              /* current pixels */
  165.    struct point old;              /* old pixels */
  166.    struct point oldlast;          /* old pixels */
  167.  
  168.    static struct point bad;          /* out of range value */
  169.  
  170.    struct f_point f_cur;
  171.    struct f_point f_old;
  172.    static struct f_point f_bad;       /* out of range value */
  173.  
  174.    static float far *fpixels;          /* float persion of pixels array */
  175.  
  176.    VECTOR v;                  /* double vector */
  177.    VECTOR v1,v2;
  178.    VECTOR crossavg;
  179.    char crossnotinit;              /* flag for crossavg init indication */
  180.  
  181.    static VECTOR light_direction;
  182.    double v_length;
  183.    VECTOR origin, direct;
  184.    LVECTOR lv;                      /* long equivalent of v */
  185.    LVECTOR lv0;                   /* long equivalent of v */
  186.    int color;                  /* current decoded color */
  187.    /* corners of transformed xdotx by ydots x colors box */
  188.    double xmin, ymin, zmin, xmax, ymax, zmax;
  189.    int i,j;
  190.    int lastdot;
  191.    int rownum;
  192.  
  193.    ENTER_OVLY(OVLY_LINE3D);
  194.  
  195.    if(transparent[0] || transparent[1])
  196.       plot = normalplot = T_clipcolor;    /*  Use the transparent plot function */
  197.    else if(colors >= 16)
  198.       plot = normalplot = clipcolor;    /* the usual FRACTINT plot function with clipping */
  199.    else
  200.       plot = normalplot = clipcolor2;
  201.  
  202.    rownum = rowcount;
  203.    if (pot16bit)
  204.       rownum >>= 1;
  205.  
  206.    /*
  207.       This IF clause is executed ONCE per image. All precalculations are
  208.       done here, with out any special concern about speed. DANGER -
  209.       communication with the rest of the program is generally via static
  210.       or global variables.
  211.    */
  212.    if(rowcount++ == 0)
  213.    {
  214.  
  215.  
  216.       long check_extra;
  217.       float theta,theta1,theta2;    /* current,start,stop latitude */
  218.       float phi1,phi2;            /* current start,stop longitude */
  219.       float   deltatheta;        /* increment of latitude */
  220.       /* plot_setup();*/
  221.       /* lastrow stores the previous row of the original GIF image for
  222.      the purpose of filling in gaps with triangle procedure */
  223.  
  224.     calctime = evenoddrow = 0;
  225.     calc_status = 1; /* mark as in-progress, and enable <tab> timer display */
  226.     if (dotmode == 11) {
  227.        movecursor(2,0);
  228.        printf("...mapping to 3d...");
  229.        }
  230.  
  231.     IAmbient = (unsigned int) 255 * (float)(100 - Ambient) / 100.0;
  232.     if (full_color && FILLTYPE >= 5)
  233.     {
  234.  
  235.     restart:
  236.  
  237.     strcpy(openfile,light_name);
  238.     strcpy(lightfiletype,".tga");    /*  Assume file ext. is ".tga"   */
  239.     for (i = 0; i < strlen(openfile); i++)    /*  See if they supplied  */
  240.         if (openfile[i] == '.')             /*  an extention          */
  241.         {                    /*  Yes!          */
  242.         strcpy(lightfiletype,&openfile[i]);  /*  Capture it      */
  243.         openfile[i] = 0;    /*    Del. the ext. from the filename   */
  244.         }
  245.     if (++numsaves1 > 1) /*  Have we already saved this file on      */
  246.     {   /*    Yes!  */
  247.         updatesavename(openfile);     /* secondary saves? new filename */
  248.         strncpy(light_name, openfile, strlen(openfile));
  249.     }
  250.     strcat(openfile,lightfiletype); /* Rebuild filename with ext.      */
  251.       /*  Does file name exist?  */
  252.     if (warn && (out1=fopen(openfile,"r")) != NULL)
  253.     {   /*    Oops!  */
  254.         fclose(out1);
  255.         goto restart;
  256.     }
  257.     startdisk1(light_name);
  258.     }
  259.     rand_factor = 14 - RANDOMIZE;
  260.  
  261.       if(colors<16)
  262.      zcoord = 256;
  263.       else
  264.      zcoord = colors;
  265.       crossavg[0] = 0;
  266.       crossavg[1] = 0;
  267.       crossavg[